var Packet = {
    pack: function(data) {
        var data_buff = msgpack.pack(data);
        var str_buff = String.fromCharCode.apply(null, new Uint8Array(data_buff));
        var len = str_buff.length + 4;
        var buf = new ArrayBuffer(len);
        var buff_data = new DataView(buf, 0, len);
        var str_len = str_buff.length;
        buff_data.setUint32(0, str_len);
        for(var i=0; i<str_len; i++) {
            buff_data.setInt8(i + 4, str_buff.charCodeAt(i));
        }

        return buf;
    },
    unpack: function(data) {
        var len = new DataView(data, 0, 4).getUint32();
        var body_data = new DataView(data, 4, len);
        var body = '';
        for(var i=0; i<body_data.byteLength; i++) {
            body += String.fromCharCode(body_data.getUint8(i));
        }
        body = msgpack.unpack(body);
        body['len'] = len;
        return body;
    }
}